package com.example.exchangeapp.zjs;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;
import com.example.exchangeapp.R;

import java.util.ArrayList;
import java.util.List;

public class ProductAdapter extends RecyclerView.Adapter<ProductAdapter.ProductViewHolder> {

    private List<Product> productList;

    public ProductAdapter(List<Product> productList) {
        if (productList == null) {
            this.productList = new ArrayList<>(); // 初始化一个空列表，避免空指针异常
        } else {
            this.productList = productList;
        }
    }

    @NonNull
    @Override
    public ProductViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_product, parent, false);
        return new ProductViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ProductViewHolder holder, int position) {
        Product product = productList.get(position);

        // 设置商品图片、商品名称和价格等信息
        if (product.getImageUrlList() == null || product.getImageUrlList().size()==0) {
            // 如果没有商品图片URL，则显示默认本地图片，或隐藏productImageView
            holder.productImageView.setImageResource(R.drawable.default_product_image);
            //holder.productImageView.setVisibility(View.GONE);
        } else {
            String imageUrl = product.getImageUrlList().get(0); // 获取商品图片URL集合中的第一个元素
            // 使用Glide加载图片到productImageView中
            Glide.with(holder.itemView.getContext())
                    .load(imageUrl)
                    .into(holder.productImageView);
        }
        //holder.productNameTextView.setText(product.getContent());
        holder.productPriceTextView.setText((String.valueOf(product.getPrice())));

        // 设置商品项的点击事件
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int clickedPosition = holder.getAdapterPosition();
                if (clickedPosition != RecyclerView.NO_POSITION) {
                    // 获取点击的商品信息
                    Product clickedProduct = productList.get(clickedPosition);

                    // 创建Intent，将商品信息传递给ProductDetailActivity，并启动该Activity
                    Intent intent = new Intent(view.getContext(), ProductDetailActivity.class);
                    intent.putExtra("product", clickedProduct);
                    view.getContext().startActivity(intent);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        System.out.println("ProductAdapter中商品数据有"+productList.size());
        return productList.size();
    }

    static class ProductViewHolder extends RecyclerView.ViewHolder {
        ImageView productImageView;
        //TextView productNameTextView;
        TextView productPriceTextView;

        public ProductViewHolder(@NonNull View itemView) {
            super(itemView);
            productImageView = itemView.findViewById(R.id.productImageView);
            //productNameTextView = itemView.findViewById(R.id.productNameTextView);
            productPriceTextView = itemView.findViewById(R.id.productPriceTextView);
        }
    }
}

